package edu.nf.project.configs;

import jakarta.servlet.MultipartConfigElement;
import jakarta.servlet.ServletRegistration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * Web初始化器
 */
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 获取根配置类
     * @return 根配置类的Class对象
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    /**
     * 获取Servlet配置类
     * @return Servlet配置类的Class对象
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{RootConfig.class};
    }

    /**
     * 获取Servlet映射
     * @return 根地址
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 获取Servlet名称
     * @return Servlet名称
     */
    @Override
    protected String getServletName() {
        return "dispatcher";
    }

    /**
     * 自定义servlet配置 - 给DispatcherServlet
     * 例如启用DIspatcherServlet上传功能
     */
    protected void customizeRegistration(ServletRegistration.Dynamic registration){
        MultipartConfigElement configElement = new MultipartConfigElement("",20971520,1024L*1024*1024*2,2097152);

        registration.setMultipartConfig(configElement);

        registration.setLoadOnStartup(0);
    }
}
